//
//  CollectViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "DateBaseSingleton.h"
#import "Movie.h"
#import "MovieDataViewController.h"

@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *myTableView;
@property(nonatomic, retain)NSMutableArray *array;

@property(nonatomic, retain)UITableViewCell *cell;

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.myTableView reloadData];
    self.array = [NSMutableArray array];
    self.array = [[DateBaseSingleton shareDataBase]selectCollect:[[NSUserDefaults standardUserDefaults] objectForKey:@"user"]];
    
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.array.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *filePath = @"cell";
    self.cell = [self.myTableView dequeueReusableCellWithIdentifier:filePath];

    self.cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    return self.cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    MovieDataViewController *movieDataVC = [[MovieDataViewController alloc] init];
    NSMutableArray *array = [NSMutableArray array];
    array = [[DateBaseSingleton shareDataBase] selectMovieCollect];
    
    movieDataVC.movieID = [array objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:movieDataVC animated:YES];
    [movieDataVC release];
    
    

}



-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    
    [self.myTableView setEditing:editing animated:animated];
    
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    // 如果是进行删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 特别注意：一定是先删数据再删cell
        [[DateBaseSingleton shareDataBase]deleteMovie:[self.array objectAtIndex:indexPath.row]];
        // 根据row删除学生对象
        [self.array removeObjectAtIndex:indexPath.row];
        // 删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation: UITableViewRowAnimationBottom];
        
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return @"确认删除";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
